import requests
import json
from pyquery import PyQuery as jq
import pandas as pd
url = "http://zu.fang.com/house/i33/"
data = ""
headers = json.loads(r'''{
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "global_cookie=ucf134re8qhlxv1iqdmcyau9417jl5wsbk7; Integrateactivity=notincludemc; indexAdvLunbo=lb_ad1%2C0%7Clb_ad2%2C0; unique_cookie=U_t7vgca0xl22z99gk83picg6bk1kjlzzbovt*1; integratecover=1; SoufunSessionID_Rent=1_1536807200_1852; ASP.NET_SessionId=prekh4ahza3szljr2bvg0212; polling_imei=2bc74439d0d8e2b3; city=www; Captcha=526772496841504F306B534647644E514F3671462B565A6652614C72775331504A7A5A6B58795153417561466D44363465663750506D54546E3170554A5479713469795A64752F6E6B534D3D",
    "Host": "zu.fang.com",
    "Referer": "http://zu.fang.com/house/i32/",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
}''')


datacol = {0:'标题',1:'类型',2:'地址',3:'距离',4:'价格',5:'标签'}
class room():

	def __init__(self):
		self.url = "http://zu.fang.com"
		self.alllist = []

	def getList(self,urls):
		resp = requests.get(urls, data=data, headers=headers)
		lists = jq(resp.text)('#listBox > div.houseList')
		for i in lists('dl').items():
			title = i('dd > .title').text()
			types = i('dd > .font15.mt12.bold').text()
			place = i('dd > .gray6.mt12').text()
			distance = i('dd > div:nth-child(4)').text()
			price = i('dd > .moreInfo').text()
			biaoqian = i('dd > .mt12').text()

			self.alllist.append([title,types,place,distance,price,biaoqian])


	def start(self):
		for i in range(99):
			url = self.url + "/house/i3{i}";
			self.getList(url)
		with open('ftx.json','w',encoding='utf-8') as f:
			json.dump(self.alllist, f)
		df = pd.DataFrame(self.alllist)
		df.rename(columns=datacol,inplace=True)
		df.to_excel('ftx.xlsx')


if __name__ == '__main__':

	room().start()
    